<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="padding: 10px 0">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog=true"
              >新增角色</el-button>
            </el-row>
            <!-- 新增/编辑弹窗 -->
            <el-dialog :title="roleForm.id?'编辑':'新增'" :visible="showDialog" @close="closeDilog">
              <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
                <el-form-item label="角色名称" prop="name">
                  <el-input v-model="roleForm.name" />
                </el-form-item>
                <el-form-item label="角色描述">
                  <el-input v-model="roleForm.description" />
                </el-form-item>
              </el-form>
              <!-- 底部 -->
              <el-row slot="footer" type="flex" justify="center">
                <el-col :span="6">
                  <el-button size="small" @click="closeDilog">取消</el-button>
                  <el-button size="small" type="primary" @click="btnOK">确定</el-button>
                </el-col>
              </el-row>
            </el-dialog>
            <!-- 表格 -->
            <el-table :data="roleList">
              <el-table-column label="序号" type="index" width="120" />
              <el-table-column label="角色名称" prop="name" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column #default="{row}" label="操作">
                <el-button size="small" type="success" @click="permSelect(row.id)">分配权限</el-button>
                <el-button size="small" type="primary" @click="fillBack(row.id)">编辑</el-button>
                <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination layout="prev,pager,next" :total="pages.total" :current-page="pages.page" :page-size="pages.pagesize" @current-change="changePage" />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <PermCheck ref="permC" :show-assign-dialog.sync="showAssignDialog" />
  </div>
</template>

<script>
import { getRoleListAPI, deleteRoleAPI, addRole, getRoleDetail, updateRole } from '@/api/setting.js'
import PermCheck from './components/permissionChecked.vue'
export default {
  components: {
    PermCheck
  },
  data() {
    return {
      // 控制分配角色的弹窗
      showAssignDialog: false,
      // 控制弹窗
      showDialog: false,
      // 传参、赋值使用
      roleForm: {
        name: '',
        description: ''
      },
      roleList: [],
      pages: {
        page: 1,
        total: 0,
        pagesize: 10
      },
      // 申明校验规则
      rules: {
        name: [
          { required: true, message: '请输入角色名', trigger: 'blur' },
          { min: 2, max: 10, message: '名称长度为2~10', trigger: ['blur', 'change'] }]
      }
    }
  },
  // 组件创建时调用，调用接口渲染数据
  created () {
    this.getRoleList()
  },
  methods: {
    // 给角色分配权限
    permSelect(id) {
      this.showAssignDialog = true
      this.$refs.permC.recoverChecked(id)
    },
    async getRoleList() {
      const { rows, total } = await getRoleListAPI(this.pages)
      console.log(rows, total)
      this.roleList = rows
      this.pages.total = total
    },
    // 分页被点击时触发，
    changePage(id) {
      this.pages.page = id
      this.getRoleList()
    },
    // 删除角色
    async delRole(id) {
      try {
        await this.$confirm('确认删除吗？', '提示')
        await deleteRoleAPI(id)
        // 重新刷新页面
        this.getRoleList()
        this.$message.success('删除成功！')
      } catch (error) {
        console.log(error)
      }
    },
    // 关闭弹窗
    closeDilog() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    // 新增角色
    btnOK() {
      this.$refs.roleForm.validate(isOK => {
        if (isOK) {
          if (this.roleForm.id) {
            updateRole(this.roleForm).then(res => {
              // 校验通过
              this.getRoleList()
              this.$message.success('操作成功!', '提示')
              // 关闭弹窗
              this.showDialog = false
            })
          } else {
            addRole(this.roleForm).then(res => {
              // 校验通过
              this.getRoleList()
              this.$message.success('操作成功!', '提示')
              this.showDialog = false
            })
          }
        }
      })
    },
    async fillBack(id) {
      this.showDialog = true
      const res = await getRoleDetail(id)
      console.log(res)
      this.roleForm = res
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
